package com.sakura.mybatis.version3.doctor.utils;

import com.alibaba.excel.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Sakura
 * @Date 2023/6/25 1:44
 * @Version 1.0
 */
public class FileUtils {

    /**
     * 19      * 列出目录下的所有文件.
     * 20      *
     * 21      * @param path
     * 22      * @return
     * 23
     */
    public static List<File> getAllFiles(String path) {
        List<File> files = new ArrayList<File>();
        if (StringUtils.isBlank(path)) {
            throw new RuntimeException("文件路径为空!");
        }
        File root = new File(path);
        if (root.exists()) {
            if (root.isDirectory()) {
                File[] childFiles = root.listFiles();
                for (File childFile : childFiles) {
                    files.addAll(getAllFiles(childFile.getAbsolutePath()));
                }
            } else {
                files.add(root);
            }
        }
        return files;
    }


    public static List<String> getLevel1Files(String path) {
        List<String> files = new ArrayList<>();
        if (StringUtils.isBlank(path)) {
            throw new RuntimeException("文件路径为空!");
        }
        File root = new File(path);
        if (root.isDirectory()) {
            File[] subDirectories = root.listFiles(File::isDirectory);
            for (File subDirectory : subDirectories) {
                files.add(subDirectory.getName());
            }
        }
        return files;
    }
}
